package com.example.villagemarket;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;




public class JsonMenuParser
{

    public HashMap<String, String> parse(String stringToDecode) throws JSONException
    {
    	HashMap<String, String> toReturn = new HashMap<String, String>();
    	
    	// Creating a JSONArray from a String 
    	JSONArray nodeRoot  = new JSONArray(stringToDecode); 
    	
    	for(int i = 0; i < nodeRoot.length(); i++)
    	{
    		
    		JSONObject jso = nodeRoot.getJSONObject(i);
    		JSONArray jsa = jso.names();
    		
    		String item_category = jsa.getString(0).substring(0,3);

    		String tempStorage = "";
    		if(item_category.equals("hot"))
    		{	
    			
    			tempStorage = toReturn.get("Hot Soups");
    			//Implies it is the first item
    			if(tempStorage==null)
    			{
    				tempStorage = jso.getString(jsa.getString(0));
    			}
    			else
    			{
    				tempStorage = toReturn.get("Hot Soups");
    				tempStorage = tempStorage+"~"+jso.getString(jsa.getString(0));
    			}
    			
    			toReturn.put("Hot Soups", tempStorage);
    		}
    		else if(item_category.equals("col"))
    		{
    			tempStorage = toReturn.get("Cold Soups");
    			//Implies it is the first item
    			if(tempStorage==null)
    			{
    				tempStorage = jso.getString(jsa.getString(0));
    			}
    			else
    			{
    				tempStorage = toReturn.get("Cold Soups");
    				tempStorage = tempStorage+"~"+jso.getString(jsa.getString(0));
    			}
    			toReturn.put("Cold Soups", tempStorage);
    		}
    		else if(item_category.equals("san"))
    		{
    			tempStorage = toReturn.get("Sandwiches");
    			//Implies it is the first item
    			if(tempStorage==null)
    			{
    				tempStorage = jso.getString(jsa.getString(0));
    				
    			}
    			else
    			{
    				tempStorage = toReturn.get("Sandwiches");
    				tempStorage = tempStorage+"~"+jso.getString(jsa.getString(0));
    			}
    			toReturn.put("Sandwiches", tempStorage);
       			
    		}
    		else if(item_category.equals("sal"))
    		{
    			tempStorage = toReturn.get("Salads & Sides");
    			
    			//Implies it is the first item
    			if(tempStorage==null)
    			{
    				tempStorage = jso.getString(jsa.getString(0));
    			}
    			else
    			{
    				tempStorage = toReturn.get("Salads & Sides");
    				tempStorage = tempStorage+"~"+jso.getString(jsa.getString(0));
    			}
    			
    			toReturn.put("Salads & Sides", tempStorage);
    		}
    		else if(item_category.equals("bak"))
    		{
    			tempStorage = toReturn.get("Bakery");
    			//Implies it is the first item
    			if(tempStorage==null)
    			{
    				tempStorage = jso.getString(jsa.getString(0));
    			}
    			else 
    			{
    				tempStorage = toReturn.get("Bakery");
    				tempStorage = tempStorage+"~"+jso.getString(jsa.getString(0));
    			}
    			toReturn.put("Bakery", tempStorage);
    		}
    		
			

    	}


    	
    	 
    	return toReturn;
    	
    	
        
    }
}